
import { defineStore } from 'pinia'
import { useUserStore}  from "./user.ts";
const userStore = useUserStore()
export const useWebsocket = defineStore(
    'websocket',
    () => {
        let websocket : WebSocket | null = null;
//连接websocket
        const connectWebSocket = () => {
            websocket = new WebSocket("ws://localhost:8889/ws");
            websocket.send(JSON.stringify({type: 1, userId: userStore.currentLoginUserInfo.id}));
            websocket.onerror = (event) => {
                console.error('WebSocket error:', event);
            };
        };
        return {
            websocket,
            connectWebSocket
        }
    },
    {
        persist: {
            key: 'websocket'
        }
    },
)
